﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public interface X
    {
        int f();
    }

    public class Class1
    {
        private X x;
        public Class1(X x)
        {
            this.x = x;
        }
        public virtual int g()
        {
            return 2 * x.f();
        }
    }
    public class Class2
    {
        Class1 c;
        public Class2(Class1 c)
        {
            this.c = c;
        }
        public string h(int n)
        {
            return string.Format("{0}:{1}", n, c.g());
        }
    }
    public interface IMessageSource : IObservable<string>, IDisposable
    {
        string Name { get; }
        void Start();
        void Stop();
    }
    public enum PersistResult
    {
        OK,
        SystemException,
        ApplicationException,
        BusinessException
    }
    public interface IMessageTarget : IDisposable
    {
        string Name { get; }
        void Start();
        void Stop();
        PersistResult Write(string message);
        void CheckHealth();
    }
    public interface IMessageWriter
    {
        string ID { get; }
        string Host { get; }
        string Name { get; }
        string SourceName { get; }
        string TargetName { get; }
        string TargetMapVersion { get; }
        void Start();
        void Stop();
    }
    public class MessageWriter : IObserver<string>, IMessageWriter
    {
        IMessageSource source;
        IMessageTarget target;
        IDisposable subscription;
        public MessageWriter(IMessageSource s, IMessageTarget t)
        {
            source = s;
            target = t;
        }
        public string ID { get; set; }

        public string Host { get; set; }

        public string Name { get; set; }

        public string SourceName { get; set; }

        public string TargetName { get; set; }

        public string TargetMapVersion { get; set; }

        public void Start()
        {
            target.Start();
            subscription = source.Subscribe(this);
            source.Start();
        }

        public void Stop()
        {
            subscription.Dispose();
            source.Stop();
            target.Stop();
        }

        public void OnNext(string message)
        {
            target.Write(message);
        }

        public void OnCompleted()
        {
            throw new NotImplementedException();
        }

        public void OnError(Exception error)
        {
            throw new NotImplementedException();
        }
    }
}